package Other;

/**
 * @author:gao
 * @date:2020/7/9
 * @time:18:11
 * @version:0.0.1
 * @description:none
 */

class Trie {
    private Trie[] next;
    private boolean isEnd;

    /** Initialize your data structure here. */
    public Trie() {
        next = new Trie[26];
        isEnd =false;
    }

    /** Inserts a word into the trie. */
    public void insert(String word) {
        Trie cur = this;
        for(char c: word.toCharArray()){
            int index = c-'a';
            if(cur.next[index]==null) cur.next[index] = new Trie();
            cur = cur.next[index];
        }
        cur.isEnd = true;
    }

    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        Trie cur = this;
        for (char c: word.toCharArray()){
            int index = c-'a';
            if(cur.next[index]==null) return false;
            cur =cur.next[index];
        }
        if(cur.isEnd) return true;
        return false;
    }

    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        Trie cur = this;
        for (char c: prefix.toCharArray()){
            int index = c-'a';
            if(cur.next[index]==null) return false;
            cur =cur.next[index];
        }
        return true;
    }
}